package cn.terminus.biz.customer.customreceive;

import cn.terminus.biz.customer.common.AbstractTerminusManager;
import cn.terminus.common.customer.bean.customreceive.dto.*;
import cn.terminus.data.customer.customreceive.dao.CustomReceiveDao;
import cn.terminus.result.common.exception.DataAccessException;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.result.common.util.BeanCopyUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * (CustomReceive)表服务实现类
 *
 * @author makejava
 * @since 2021-08-12 10:27:51
 */
@Component
public class CustomReceiveManager extends AbstractTerminusManager {
    @Autowired
    private CustomReceiveDao customReceiveDao;

    public Integer add(CustomReceiveNewDto dto) throws DataAccessException {
        try {
            return customReceiveDao.add(dto);
        } catch (Exception e) {
            logger.error("CustomReceiveManager新增失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer update(CustomReceiveEditDto dto) throws DataAccessException {
        try {
            return customReceiveDao.update(dto);
        } catch (Exception e) {
            logger.error("CustomReceiveManager更新失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomReceiveResultDto> gets(CustomReceiveGetsDto dto) throws DataAccessException {
        try {
            return customReceiveDao.gets(dto);
        } catch (Exception e) {
            logger.error("CustomReceiveManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public List<CustomReceiveResultDto> query(CustomReceiveQueryDto dto) throws DataAccessException {
        try {
            return customReceiveDao.query(dto);
        } catch (Exception e) {
            logger.error("CustomReceiveManager查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer delete(CustomReceiveDeleteDto dto) throws DataAccessException {
        try {
            return customReceiveDao.delete(dto);
        } catch (Exception e) {
            logger.error("CustomReceiveManager删除失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public TerminusPageAble<CustomReceiveResultDto> page(CustomPageQueryDto dto, TerminusPageInfo page) throws DataAccessException {
        try {
            String orderBy = "status asc,create_time desc";
            PageMethod.startPage(page.getCurrentPage(), page.getPageSize(), orderBy);
            Page<CustomReceiveResultDto> result = (Page<CustomReceiveResultDto>) customReceiveDao.queryPage(dto);
            return new TerminusPageAble<>(BeanCopyUtils.copy(result.getResult(), CustomReceiveResultDto.class), page, result.getTotal());
        } catch (Exception e) {
            logger.error("CustomReceiveManager分页查询失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer batchConfig(BatchConfigDto configDto) throws DataAccessException {
        try {
            return customReceiveDao.batchConfig(configDto);
        } catch (Exception e) {
            logger.error("批量确认失败，原因：", e);
            throw new DataAccessException(e);
        }
    }

    public Integer insertBatch(List<CustomReceiveNewDto> receiveNewDtoList) throws DataAccessException {
        try {
            return customReceiveDao.insertBatch(receiveNewDtoList);
        } catch (Exception e) {
            logger.error("批量insert，原因：", e);
            throw new DataAccessException(e);
        }

    }
}
